package DiffieHellman.TestSM2;

import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.math.raw.Nat256;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;

public class TestSM2FieldElement extends ECFieldElement.AbstractFp {
    public static final BigInteger Q = new BigInteger(1, Hex.decodeStrict("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3"));
    protected int[] x;

    public TestSM2FieldElement(BigInteger var1) {
        if (var1 != null && var1.signum() >= 0 && var1.compareTo(Q) < 0) {
            this.x = TestSM2Field.fromBigInteger(var1);
        } else {
            throw new IllegalArgumentException("x value invalid for SM2P256V1FieldElement");
        }
    }

    public TestSM2FieldElement() {
        this.x = Nat256.create();
    }

    protected TestSM2FieldElement(int[] var1) {
        this.x = var1;
    }

    public boolean isZero() {
        return Nat256.isZero(this.x);
    }

    public boolean isOne() {
        return Nat256.isOne(this.x);
    }

    public boolean testBitZero() {
        return Nat256.getBit(this.x, 0) == 1;
    }

    public BigInteger toBigInteger() {
        return Nat256.toBigInteger(this.x);
    }

    public String getFieldName() {
        return "SM2P256V1Field";
    }

    public int getFieldSize() {
        return Q.bitLength();
    }

    public ECFieldElement add(ECFieldElement var1) {
        int[] var2 = Nat256.create();
        TestSM2Field.add(this.x, ((DiffieHellman.TestSM2.TestSM2FieldElement) var1).x, var2);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var2);
    }

    public ECFieldElement addOne() {
        int[] var1 = Nat256.create();
        TestSM2Field.addOne(this.x, var1);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var1);
    }

    public ECFieldElement subtract(ECFieldElement var1) {
        int[] var2 = Nat256.create();
        TestSM2Field.subtract(this.x, ((DiffieHellman.TestSM2.TestSM2FieldElement) var1).x, var2);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var2);
    }

    public ECFieldElement multiply(ECFieldElement var1) {
        int[] var2 = Nat256.create();
        TestSM2Field.multiply(this.x, ((DiffieHellman.TestSM2.TestSM2FieldElement) var1).x, var2);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var2);
    }

    public ECFieldElement divide(ECFieldElement var1) {
        int[] var2 = Nat256.create();
        TestSM2Field.inv(((DiffieHellman.TestSM2.TestSM2FieldElement) var1).x, var2);
        TestSM2Field.multiply(var2, this.x, var2);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var2);
    }

    public ECFieldElement negate() {
        int[] var1 = Nat256.create();
        TestSM2Field.negate(this.x, var1);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var1);
    }

    public ECFieldElement square() {
        int[] var1 = Nat256.create();
        TestSM2Field.square(this.x, var1);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var1);
    }

    public ECFieldElement invert() {
        int[] var1 = Nat256.create();
        TestSM2Field.inv(this.x, var1);
        return new DiffieHellman.TestSM2.TestSM2FieldElement(var1);
    }

    public ECFieldElement sqrt() {
        int[] var1 = this.x;
        if (!Nat256.isZero(var1) && !Nat256.isOne(var1)) {
            int[] var2 = Nat256.create();
            TestSM2Field.square(var1, var2);
            TestSM2Field.multiply(var2, var1, var2);
            int[] var3 = Nat256.create();
            TestSM2Field.squareN(var2, 2, var3);
            TestSM2Field.multiply(var3, var2, var3);
            int[] var4 = Nat256.create();
            TestSM2Field.squareN(var3, 2, var4);
            TestSM2Field.multiply(var4, var2, var4);
            TestSM2Field.squareN(var4, 6, var2);
            TestSM2Field.multiply(var2, var4, var2);
            int[] var6 = Nat256.create();
            TestSM2Field.squareN(var2, 12, var6);
            TestSM2Field.multiply(var6, var2, var6);
            TestSM2Field.squareN(var6, 6, var2);
            TestSM2Field.multiply(var2, var4, var2);
            TestSM2Field.square(var2, var4);
            TestSM2Field.multiply(var4, var1, var4);
            TestSM2Field.squareN(var4, 31, var6);
            TestSM2Field.multiply(var6, var4, var2);
            TestSM2Field.squareN(var6, 32, var6);
            TestSM2Field.multiply(var6, var2, var6);
            TestSM2Field.squareN(var6, 62, var6);
            TestSM2Field.multiply(var6, var2, var6);
            TestSM2Field.squareN(var6, 4, var6);
            TestSM2Field.multiply(var6, var3, var6);
            TestSM2Field.squareN(var6, 32, var6);
            TestSM2Field.multiply(var6, var1, var6);
            TestSM2Field.squareN(var6, 62, var6);
            TestSM2Field.square(var6, var3);
            return Nat256.eq(var1, var3) ? new DiffieHellman.TestSM2.TestSM2FieldElement(var6) : null;
        } else {
            return this;
        }
    }

    public boolean equals(Object var1) {
        if (var1 == this) {
            return true;
        } else if (!(var1 instanceof DiffieHellman.TestSM2.TestSM2FieldElement)) {
            return false;
        } else {
            DiffieHellman.TestSM2.TestSM2FieldElement var2 = (DiffieHellman.TestSM2.TestSM2FieldElement) var1;
            return Nat256.eq(this.x, var2.x);
        }
    }

    public int hashCode() {
        return Q.hashCode() ^ Arrays.hashCode(this.x, 0, 8);
    }
}
